Advertisement
Junaid_Hossain

Stack with Linked List 3

May 17th, 2024
546
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. struct Student {
  6.     char name[50];
  7.     int reg;
  8.     double cgpa;
  9.     int semester;
  10.     int year;
  11.     int isRegular; // irregular = 0, regular = 1
  12.     struct Student* next;
  13. };
  14.  
  15. struct Student* head = NULL;
  16.  
  17. void addNewStudent(const char* Name, int reg_no, double gpa, int sem, int years, int reg) {
  18.     struct Student* ptr = (struct Student*)malloc(sizeof(struct Student));
  19.     if (ptr == NULL) {
  20.         printf("Memory allocation failed\n");
  21.         exit(1);
  22.     }
  23.     strcpy(ptr->name, Name);
  24.     ptr->reg = reg_no;
  25.     ptr->cgpa = gpa;
  26.     ptr->semester = sem;
  27.     ptr->year = years;
  28.     ptr->isRegular = reg;
  29.     ptr->next = NULL;
  30.  
  31.     if (head == NULL || head->cgpa < ptr->cgpa) {
  32.         ptr->next = head;
  33.         head = ptr;
  34.     } else {
  35.         struct Student* temp = head;
  36.         while (temp->next != NULL && ptr->cgpa < temp->next->cgpa) {
  37.             temp = temp->next;
  38.         }
  39.         ptr->next = temp->next;
  40.         temp->next = ptr;
  41.     }
  42.     printf("New Student is Added - %s\n", ptr->name);
  43. }
  44.  
  45. void delStudent(int reg_no) {
  46.     struct Student* temp = head;
  47.     struct Student* prev = NULL;
  48.  
  49.     while (temp != NULL) {
  50.         if (temp->reg == reg_no) {
  51.             if (prev == NULL) { // Deleting head node
  52.                 head = temp->next;
  53.             } else {
  54.                 prev->next = temp->next;
  55.             }
  56.             printf("Student Record is Removed - %s\n", temp->name);
  57.             free(temp);
  58.             return;
  59.         }
  60.         prev = temp;
  61.         temp = temp->next;
  62.     }
  63.     printf("Student Record with reg number %d not found\n", reg_no);
  64. }
  65.  
  66. void displayAllStudent() {
  67.     struct Student* p = head;
  68.     while (p != NULL) {
  69.         printf("%d_%s_%.2f_%d-%d_", p->reg, p->name, p->cgpa, p->year, p->semester);
  70.         if (p->isRegular)
  71.             printf("Regular\n");
  72.         else
  73.             printf("Irregular\n");
  74.         p = p->next;
  75.     }
  76. }
  77.  
  78. struct Student* awardStack = NULL;
  79.  
  80. void pushAwardee(struct Student* student) {
  81.     struct Student* newAwardee = (struct Student*)malloc(sizeof(struct Student));
  82.     if (newAwardee == NULL) {
  83.         printf("Memory allocation failed\n");
  84.         exit(1);
  85.     }
  86.     *newAwardee = *student;
  87.     newAwardee->next = awardStack;
  88.     awardStack = newAwardee;
  89.     printf("Selected Awardee Enlisted - %s\n", student->name);
  90. }
  91.  
  92. void popAwardee() {
  93.     if (awardStack == NULL) {
  94.         printf("No awardee to remove\n");
  95.         return;
  96.     }
  97.     struct Student* temp = awardStack;
  98.     awardStack = awardStack->next;
  99.     printf("Awardee Removed - %s\n", temp->name);
  100.     free(temp);
  101. }
  102.  
  103. void displayAllAwardee() {
  104.     struct Student* temp = awardStack;
  105.     while (temp != NULL) {
  106.         printf("%d_%s_%.2f_%d-%d_", temp->reg, temp->name, temp->cgpa, temp->year, temp->semester);
  107.         if (temp->isRegular)
  108.             printf("Regular\n");
  109.         else
  110.             printf("Irregular\n");
  111.         temp = temp->next;
  112.     }
  113. }
  114.  
  115. struct Student* findStudentByName(const char* name) {
  116.     struct Student* p = head;
  117.     while (p != NULL) {
  118.         if (strcmp(p->name, name) == 0) {
  119.             return p;
  120.         }
  121.         p = p->next;
  122.     }
  123.     return NULL;
  124. }
  125.  
  126. // Circular queue for tokens
  127. struct TokenQueue {
  128.     struct Student* student;
  129.     struct TokenQueue* next;
  130. };
  131.  
  132. struct TokenQueue* front = NULL;
  133. struct TokenQueue* rear = NULL;
  134.  
  135. void newToken(struct Student* student) {
  136.     struct TokenQueue* newTokenNode = (struct TokenQueue*)malloc(sizeof(struct TokenQueue));
  137.     if (newTokenNode == NULL) {
  138.         printf("Memory allocation failed\n");
  139.         exit(1);
  140.     }
  141.     newTokenNode->student = student;
  142.     if (front == NULL) {
  143.         front = rear = newTokenNode;
  144.         newTokenNode->next = front;
  145.     } else {
  146.         rear->next = newTokenNode;
  147.         rear = newTokenNode;
  148.         rear->next = front;
  149.     }
  150.     printf("New Token Created - %s\n", student->name);
  151. }
  152.  
  153. void serveToken() {
  154.     if (front == NULL) {
  155.         printf("No tokens to serve\n");
  156.         return;
  157.     }
  158.     struct TokenQueue* temp = front;
  159.     if (front == rear) {
  160.         front = rear = NULL;
  161.     } else {
  162.         front = front->next;
  163.         rear->next = front;
  164.     }
  165.     printf("Token Served - %s\n", temp->student->name);
  166.     free(temp);
  167. }
  168.  
  169. void displayQueue() {
  170.     if (front == NULL) {
  171.         printf("Queue is empty\n");
  172.         return;
  173.     }
  174.     struct TokenQueue* temp = front;
  175.     do {
  176.         printf("%d_%s_%.2f_%d-%d_", temp->student->reg, temp->student->name, temp->student->cgpa, temp->student->year, temp->student->semester);
  177.         if (temp->student->isRegular)
  178.             printf("Regular\n");
  179.         else
  180.             printf("Irregular\n");
  181.         temp = temp->next;
  182.     } while (temp != front);
  183. }
  184.  
  185. int main() {
  186.     addNewStudent("Monica", 22201999, 3.50, 1, 2, 1);
  187.     addNewStudent("Ross", 22201000, 3.92, 1, 2, 1);
  188.     addNewStudent("Chandler", 18201000, 4.0, 1, 3, 1);
  189.     addNewStudent("Janice", 21201089, 2.52, 1, 2, 0);
  190.     addNewStudent("Joey", 20201979, 2.88, 1, 2, 0);
  191.     addNewStudent("Gunther", 19201000, 4.0, 2, 3, 0);
  192.  
  193.     printf("\n");
  194.     delStudent(21201089);
  195.     printf("\n");
  196.     displayAllStudent();
  197.     printf("\n");
  198.  
  199.     // Push "Monica", "Ross", and "Chandler" to the awardee stack
  200.     struct Student* student;
  201.  
  202.     student = findStudentByName("Monica");
  203.     if (student != NULL) pushAwardee(student);
  204.  
  205.     student = findStudentByName("Ross");
  206.     if (student != NULL) pushAwardee(student);
  207.  
  208.     student = findStudentByName("Chandler");
  209.     if (student != NULL) pushAwardee(student);
  210.  
  211.     printf("\nAll Awardees:\n");
  212.     displayAllAwardee();
  213.  
  214.     printf("\n");
  215.  
  216.     // Adding tokens for food distribution
  217.     student = findStudentByName("Monica");
  218.     if (student != NULL) newToken(student);
  219.  
  220.     student = findStudentByName("Ross");
  221.     if (student != NULL) newToken(student);
  222.  
  223.     student = findStudentByName("Chandler");
  224.     if (student != NULL) newToken(student);
  225.  
  226.     student = findStudentByName("Gunther");
  227.     if (student != NULL) newToken(student);
  228.  
  229.     student = findStudentByName("Joey");
  230.     if (student != NULL) newToken(student);
  231.  
  232.     printf("\nQueue Display:\n");
  233.     displayQueue();
  234.  
  235.     printf("\n");
  236.  
  237.     // Serving tokens
  238.     serveToken();
  239.     serveToken();
  240.  
  241.     printf("\nQueue Display after serving two tokens:\n");
  242.     displayQueue();
  243.  
  244.     return 0;
  245. }
  246.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement